

package rawengine.input;



import java.awt.Cursor;
import java.util.ArrayList;
import java.util.List;



import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

/**
 *
 * @author Fabio
 */

public class InputManager {
	
	// mouse codes
    public static final int MOUSE_MOVE_LEFT = 0;
    public static final int MOUSE_MOVE_RIGHT = 1;
    public static final int MOUSE_MOVE_UP = 2;
    public static final int MOUSE_MOVE_DOWN = 3;
    public static final int MOUSE_WHEEL_UP = 4;
    public static final int MOUSE_WHEEL_DOWN = 5;
    public static final int MOUSE_BUTTON_1 = 6;
    public static final int MOUSE_BUTTON_2 = 7;
    public static final int MOUSE_BUTTON_3 = 8;

    private static final int NUM_MOUSE_CODES = 9;

    private static final int NUM_KEY_CODES = 600;
    private GameAction[] mouseActions = new GameAction[NUM_MOUSE_CODES];

    private GameAction[] keyActions = new GameAction[NUM_KEY_CODES];
    
    

    public InputManager()
    {
    	/*
        try {
			Mouse.create();
		} catch (LWJGLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
    }
    
    /**
    Sets the cursor on this InputManager's input component.
	*/
	public void setCursor(Cursor cursor) 
	{
		if(cursor == null)
		{
			try {
				Mouse.setNativeCursor(Mouse.getNativeCursor());
			} catch (LWJGLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	    
	}



	

    public void mapToKey(GameAction gameAction, int keyCode)
    {
        keyActions[keyCode] = gameAction;
        
    }
    
    /**
    Maps a GameAction to a specific mouse action. The mouse
    codes are defined herer in InputManager (MOUSE_MOVE_LEFT,
    MOUSE_BUTTON_1, etc). If the mouse action already has
    a GameAction mapped to it, the new GameAction overwrites
    it.
     */
    public void mapToMouse(GameAction gameAction, int mouseCode)
    {
    	mouseActions[mouseCode] = gameAction;
    }
	
    /**
    Gets a List of names of the keys and mouse actions mapped
    to this GameAction. Each entry in the List is a String.
	*/
	public List<String> getMaps(GameAction gameCode) 
	{
	    ArrayList<String> list = new ArrayList<String>();
	
	    for (int i=0; i<keyActions.length; i++) {
	        if (keyActions[i] == gameCode) {
	            list.add(getKeyName(i));
	        }
	    }
	
	    for (int i=0; i<mouseActions.length; i++) {
	        if (mouseActions[i] == gameCode) {
	            list.add(getMouseName(i));
	        }
	    }
	    return list;
	}


    public void clearMap(GameAction gameAction)
    {
        for (int i=0; i < keyActions.length; i++)
        {
            if (keyActions[i] == gameAction)
            {
                keyActions[i] = null;
            }
        }
        
        for (int i=0; i<mouseActions.length; i++) 
        {
            if (mouseActions[i] == gameAction) 
            {
                mouseActions[i] = null;
            }
        }
        gameAction.reset();
    }

 

    public void resetAllGameActions()
    {
        for (int i=0; i<keyActions.length; i++)
        {
            if (keyActions[i] != null)
            {
                keyActions[i].reset();
            }
        }
        
        for (int i=0; i<mouseActions.length; i++) 
        {
            if (mouseActions[i] != null) 
            {
                mouseActions[i].reset();
            }
        }
    }

    /**
    Gets the name of a key code.
*/
    public static String getKeyName(int keyCode) {
    	return Keyboard.getKeyName(keyCode);
    }
    
    /**
    Gets the name of a mouse code.
	*/
	public static String getMouseName(int mouseCode) 
	{
	    switch (mouseCode) 
	    {
	        case MOUSE_MOVE_LEFT: return "Mouse Left";
	        case MOUSE_MOVE_RIGHT: return "Mouse Right";
	        case MOUSE_MOVE_UP: return "Mouse Up";
	        case MOUSE_MOVE_DOWN: return "Mouse Down";
	        case MOUSE_WHEEL_UP: return "Mouse Wheel Up";
	        case MOUSE_WHEEL_DOWN: return "Mouse Wheel Down";
	        case MOUSE_BUTTON_1: return "Mouse Button 1";
	        case MOUSE_BUTTON_2: return "Mouse Button 2";
	        case MOUSE_BUTTON_3: return "Mouse Button 3";
	        default: return "Unknown mouse code " + mouseCode;
	    }
	}
    

    /**
    Gets the x position of the mouse.
	*/
	public int getMouseX() {
	    return Mouse.getX();
	}
	
	
	/**
	    Gets the y position of the mouse.
	*/
	public int getMouseY() {
	    return Mouse.getY();
	}

	

    public GameAction getKeyAction(int KeyCode)
    {
        if (KeyCode < keyActions.length)
        {
            return keyActions[KeyCode];
        } else {
                   return null;
                }

    }

    
    
    public void KeyPressed(int KeyCode)
    {
        boolean isPressed = Keyboard.isKeyDown(KeyCode);
        GameAction gameAction = getKeyAction(KeyCode);
        if (isPressed)
        {
            if (gameAction != null)
            {
                gameAction.press();
                gameAction.release();
            }
             
            
        }
    }

    public void KeyReleased(int KeyCode)
    {
        KeyPressed(KeyCode);
    }

}


